package com.zx.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.platform.dao.SysParamDao;
import com.zx.platform.dao.model.SysParam;
import com.zx.platform.dao.model.SysParamExample;
import com.zx.platform.service.SysParamService;
import com.zx.platform.service.domain.SysParamDomain;
import com.zx.platform.service.model.SysParamModel;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("sysParamService")
public class SysParamServiceImpl implements SysParamService {

	@Autowired
	private SysParamDao dao;

	/**
	 * 得到所有数据列表
	 */
	public List getAllList() throws DataAccessException {

		List<SysParam> list = dao.getListByExample(null);
		List<SysParamModel> modelList = new ArrayList<SysParamModel>();
		for (SysParam o : list) {
			SysParamModel temp = new SysParamModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;

	}

	/**
	 * 根据单Key查找Model
	 */
	public SysParamModel getModelByKey(Integer key) throws DataAccessException {
		SysParam o = dao.selectByPrimaryKey(key);
		SysParamModel model = new SysParamModel();
		BeanUtils.copyProperties(o, model);
		return model;
	}

	/**
	 * 根据Model查找列表
	 */
	public List getListByModel(SysParamModel model) throws DataAccessException {
		SysParamExample example = new SysParamExample();
		com.zx.platform.dao.model.SysParamExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getParamKey())) {
			criteria.andParamKeyLike('%'+model.getParamKey()+'%');
		}
		if (StringUtils.isNotBlank(model.getParamName())) {
			criteria.andParamNameLike('%'+model.getParamName()+'%');
		}
		List<SysParam> list = dao.getListByExample(example);
		List<SysParamModel> modelList = new ArrayList<SysParamModel>();
		for (SysParam o : list) {
			SysParamModel temp = new SysParamModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	/**
	 * 新增
	 */
	public Integer add(SysParamModel model) throws DataAccessException {
		SysParam temp = new SysParam();
		BeanUtils.copyProperties(model, temp);
		Integer key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public Integer update(SysParamModel model) throws DataAccessException {
		SysParam temp = new SysParam();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getParamId();
	}

	/**
	 * 删除
	 */
	public void delete(SysParamModel model) throws DataAccessException {
		SysParam temp = new SysParam();
		BeanUtils.copyProperties(model, temp);
		dao.delete(temp);
	}

	/**
	 * 分页查询
	 * 
	 * @param pageSize
	 * @param pageNum
	 * @param menuModel
	 * @return
	 * @throws DataAccessException
	 */
	public Map getPageByDomain(int pageSize, int pageNum, SysParamDomain domain)
			throws DataAccessException {

		Map<String, Object> map = new HashMap<String, Object>();
		SysParamExample example = new SysParamExample();
		com.zx.platform.dao.model.SysParamExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(domain.getParamKey())) {
			criteria.andParamKeyLike('%'+domain.getParamKey()+'%');
		}
		if (StringUtils.isNotBlank(domain.getParamName())) {
			criteria.andParamNameLike('%'+domain.getParamName()+'%');
		}
		//排序add by wenjw 2015.6.2
		example.setOrderByClause("create_Date desc");
		int pageBegin = (pageNum - 1) * pageSize;
		List<SysParam> list = dao.getPageByExample(pageSize, pageBegin, example);
		List<SysParamModel> menuModelList = new ArrayList<SysParamModel>();
		for (SysParam o : list) {
			SysParamModel temp = new SysParamModel();
			BeanUtils.copyProperties(o, temp);
			menuModelList.add(temp);
		}
		int count = dao.countByExample(example);
		map.put("count", count);
		map.put("list", menuModelList);
		return map;
	}
}
